package com.micangcaihang.common.base.activity;

import android.databinding.ViewDataBinding;
import android.support.annotation.IdRes;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;

import com.micangcaihang.common.R;
import com.micangcaihang.common.base.app.BaseApp;
import com.micangcaihang.common.base.mvp.BasePresenter;
import com.micangcaihang.common.base.mvp.IBaseView;
import com.micangcaihang.common.http.exception.ApiException;
import com.micangcaihang.common.utils.LoadDialogUtil;
import com.micangcaihang.common.utils.NetWorkCheckUtils;
import com.micangcaihang.common.utils.TUtil;
import com.micangcaihang.common.utils.ToastUtils;
import com.micangcaihang.common.widget.progress.ProgressFrameLayout;
import com.micangcaihang.common.widget.progress.ProgressLinearLayout;
import com.micangcaihang.common.widget.progress.ProgressRelativeLayout;

import java.util.ArrayList;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayonggmail@gmail.com
 *     time   : 2017/06/16
 *     desc   : BaseDataBindActivity
 *     version: 1.0
 * </pre>
 */

public abstract class BaseActivity<P extends BasePresenter, B extends ViewDataBinding> extends
        BaseDataBindActivity<B> implements IBaseView {
    protected String TAG = getClass().getSimpleName();
    /**
     * Presenter对象
     */
    protected P mPresenter;

    protected ProgressFrameLayout mProgressFrameLayout;

    protected ProgressLinearLayout mProgressLinearLayout;

    protected ProgressRelativeLayout mProgressRelativeLayout;

    protected SwipeRefreshLayout mSwipeRefresh;

    protected ArrayList<Integer> mIds;
    /**
     * 网络错误相关资源文件
     */
    protected int errorDrawable = R.drawable.ic_layout_error;
    protected String errorContent = null;
    protected String errorTitle = BaseApp.me().getResources().getString(R.string.progressLayoutErrorContentPlaceholder);
    protected String errorButton = BaseApp.me().getResources().getString(R.string.progressLayoutErrorButton);

    /**
     * 空视图相关资源文件
     */
    protected int emptyDrawable = R.drawable.ic_layout_empty;
    protected String emptyContent = null;
    protected String emptyTitle = BaseApp.me().getResources().getString(R.string.progressLayoutEmptyContentPlaceholder);

    /**
     * 初始化view
     */
    @Override
    public void initView() {
        mSwipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_layout);
        mProgressLinearLayout = (ProgressLinearLayout) findViewById(R.id.progress_lly_layout);
        mProgressFrameLayout = (ProgressFrameLayout) findViewById(R.id.progress_fly_layout);
        mProgressRelativeLayout = (ProgressRelativeLayout) findViewById(R.id.progress_rly_layout);
        initSwipeRefresh();
    }

    /**
     * 初始化对象
     */
    @Override
    protected void initData() {
        mIds = new ArrayList<>();
        loadDataAndUpdateView(false);
    }

    /**
     * 简单页面无需mvp就不用管此方法即可,完美兼容各种实际场景的变通
     */
    @Override
    public void initPresenter() {
        mPresenter = TUtil.getT(this, 0);
        if (mPresenter != null) {
            mPresenter.setV(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDetached();
        }
    }

    /**
     * 初始化下拉刷新
     */
    private void initSwipeRefresh() {
        if (mSwipeRefresh != null) {
            mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    loadDataAndUpdateView(true);
                }
            });
        }
    }

    /**
     * 设置空视图资源id
     *
     * @param errorDrawableId
     * @param errorTitle
     * @param errorContent
     * @param errorButton
     */
    protected void setErrorRes(@IdRes int errorDrawableId, String errorTitle, String errorContent, String errorButton) {
        this.errorDrawable = errorDrawableId;
        this.errorTitle = errorTitle;
        this.errorContent = errorContent;
        this.errorButton = errorButton;
    }

    /**
     * 更新视图控件
     *
     * @param isRefresh 新增参数，用来判断是否为下拉刷新调用，下拉刷新的时候不应该再显示加载界面和异常界面
     */
    protected void loadDataAndUpdateView(boolean isRefresh) {
        if (mPresenter != null) {
            mPresenter.loadFirstData(isRefresh);
        }
    }

    /* IBaseView interface start*/

    /**
     * 刷新时显示刷新状态
     * 并且置为不可操作状态(只可刷新一次,刷新完才可以再次刷新)
     */
    @Override
    public void showRefreshLoading() {
        if (mSwipeRefresh != null) {
            mSwipeRefresh.post(new Runnable() {
                @Override
                public void run() {
                    mSwipeRefresh.setRefreshing(true);
                }
            });
        }
    }

    /**
     * 停止刷新状态
     * 并且将刷新控制置为可操作状态
     */
    @Override
    public void finishRefresh() {
        if (mSwipeRefresh != null) {
            mSwipeRefresh.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mSwipeRefresh.setRefreshing(false);
                    mSwipeRefresh.setEnabled(true);
                }
            }, 500);
        }
    }

    /**
     * 显示加载布局(layout)
     */
    @Override
    public void showLoading() {
        if (mSwipeRefresh != null) {
            mSwipeRefresh.setEnabled(false);
        }
        if (mProgressLinearLayout != null) {
            mProgressLinearLayout.showLoading(mIds);
        }
        if (mProgressFrameLayout != null) {
            mProgressFrameLayout.showLoading(mIds);
        }
        if (mProgressRelativeLayout != null) {
            mProgressRelativeLayout.showLoading(mIds);
        }
    }

    @Override
    public void showContent() {
        if (mProgressLinearLayout != null) {
            mProgressLinearLayout.showContent();
        }
        if (mProgressFrameLayout != null) {
            mProgressFrameLayout.showContent();
        }
        if (mProgressRelativeLayout != null) {
            mProgressRelativeLayout.showContent();
        }
    }

    /**
     * 显示联网错误布局
     *
     * @param e 异常
     */
    @Override
    public void showNetError(ApiException e) {
        String errorTitleNew = errorTitle;
//        String errorContentNew = errorContent;
        if (NetWorkCheckUtils.isHttpException(e)) {
            errorTitle = getResources().getString(R.string.progressLayoutErrorTitlePlaceholder);
        } else {
            errorTitle = getResources().getString(R.string.progressLayoutErrorContentPlaceholder);
        }
        if (mSwipeRefresh != null) {
            mSwipeRefresh.setEnabled(false);
        }
        if (mProgressLinearLayout != null) {
            mProgressLinearLayout.showError(errorDrawable, errorTitle, errorContent, errorButton, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mProgressLinearLayout.showLoading(mIds);
                    loadDataAndUpdateView(false);
                }
            }, mIds);
        }

        if (mProgressFrameLayout != null) {
            mProgressFrameLayout.showError(errorDrawable, errorTitle, errorContent, errorButton, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mProgressFrameLayout.showLoading(mIds);
                    loadDataAndUpdateView(false);
                }
            }, mIds);
        }

        if (mProgressRelativeLayout != null) {
            mProgressRelativeLayout.showError(errorDrawable, errorTitle, errorContent, errorButton, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mProgressRelativeLayout.showLoading(mIds);
                    loadDataAndUpdateView(false);
                }
            }, mIds);
        }
//        this.errorContent = errorContentNew;
        this.errorTitle = errorTitleNew;
    }

    /**
     * 显示空白界面布局
     */
    @Override
    public void showEmptyLayout() {
        if (mSwipeRefresh != null) {
            mSwipeRefresh.setEnabled(true);
        }
        if (mProgressLinearLayout != null) {
            mProgressLinearLayout.showEmpty(emptyDrawable, emptyTitle, emptyContent, mIds);
        }

        if (mProgressFrameLayout != null) {
            mProgressFrameLayout.showEmpty(emptyDrawable, emptyTitle, emptyContent, mIds);
        }

        if (mProgressFrameLayout != null) {
            mProgressFrameLayout.showEmpty(emptyDrawable, emptyTitle, emptyContent, mIds);
        }
    }

    /**
     * 显示dialog加载框
     *
     * @param title 提示文字
     */
    @Override
    public void showDialogLoading(String title) {
        LoadDialogUtil.showMaterLoading(this, title);
    }

    /**
     * 隐藏dialog加载框
     */
    @Override
    public void hideDialogLoading() {
        LoadDialogUtil.hideMaterLoading();
    }

    /**
     * toast提示信息
     *
     * @param msg 提示文字
     */
    @Override
    public void showTip(String msg) {
        ToastUtils.showLong(msg);
    }
    /* IBaseView interface end*/
}
